package com.jucai.pppeiyouclient.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;

import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.utils.LogUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenjidong on 2016-02-28.
 */
public class FilterAdapter extends BaseAdapter {

    private Activity mActivity;
    private List<FilterBean> list;
    private int selectedId = -1;
    private FilterBean selectedBean = null;
    private FilterBean preBean = null;
    private boolean isDefault = true;//是否需要默认选中第一个

    public void setDefault(boolean aDefault) {
        isDefault = aDefault;
    }

    private OnFilterItemClickListener onFilterItemClickListener;

    public void setOnFilterItemClickListener(OnFilterItemClickListener onFilterItemClickListener) {
        this.onFilterItemClickListener = onFilterItemClickListener;
    }

    public FilterBean getSelectedBean() {
        return selectedBean;
    }

    public FilterAdapter(Activity mActivity) {
        this.mActivity = mActivity;
        list = new ArrayList<>();
    }

    public void setList(List<FilterBean> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    public void clearSelected() {
        selectedId = -1;
        selectedBean = null;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mActivity.getLayoutInflater().inflate(R.layout.item_filter_list, null);
            viewHolder = new ViewHolder();
            viewHolder.root = convertView;
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.cb_text);
            viewHolder.rltFilter = (RelativeLayout) convertView.findViewById(R.id.rlt_filter);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final FilterBean bean = list.get(position);
        viewHolder.checkBox.setText(bean.keyName);
        if (selectedId == bean.id) {
            viewHolder.checkBox.setChecked(true);
            viewHolder.checkBox.setTextColor(mActivity.getResources().getColor(R.color.app_theme_color_one));
//            viewHolder.rltFilter.setBackgroundResource(R.drawable.shape_filter_theme_corners100);
        } else {
            if (position == 0 && (selectedId == -1 && isDefault)) {
                selectedId = bean.id;
                selectedBean = bean;
                viewHolder.checkBox.setChecked(true);
            } else
                viewHolder.checkBox.setChecked(false);
            viewHolder.checkBox.setTextColor(mActivity.getResources().getColor(R.color.app_theme_gray));
//            viewHolder.rltFilter.setBackgroundResource(R.drawable.shape_filter_grey_corners100);

        }
        viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                preBean = selectedBean;
                selectedId = bean.id;
                selectedBean = bean;

                if (onFilterItemClickListener != null)
                    onFilterItemClickListener.onSelected(preBean, selectedBean);
                FilterAdapter.this.notifyDataSetInvalidated();
            }
        });

        return convertView;
    }


    class ViewHolder {
        View root;
        CheckBox checkBox;
        RelativeLayout rltFilter;
    }

    public static class FilterBean implements Serializable {

        public FilterBean(int id, String keyName, String valueName, String remark) {
            this.id = id;
            this.keyName = keyName;
            this.valueName = valueName;
            this.remark = remark;
        }

        public int id = -1;
        public String keyName = "";
        public String valueName = "";
        public String remark = "";
    }

    public interface OnFilterItemClickListener {
        /**
         * \
         *
         * @param preBean 上一个bean
         * @param nowBean 当前选中的bean
         */
        void onSelected(FilterBean preBean, FilterBean nowBean);
    }

}